package com.example.theadadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.theadadmin.entity.User;
import com.example.theadadmin.exception.BisnessException;
import com.example.theadadmin.mapper.UserMapper;
import com.example.theadadmin.service.IUserService;
import com.example.theadadmin.util.JWTTokenUtil;
import com.example.theadadmin.util.Md5Util;
import com.example.theadadmin.util.PwdUtil;
import com.example.theadadmin.vo.AccountVo;
import com.example.theadadmin.vo.ForgetVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 * @author 王东
 * @since 2024-09-05
 */

@Transactional
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private final UserMapper userMapper;
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private JavaMailSender mailSender;

    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    /**
     * 注册功能
     * @param count
     */
    @Override
    public void register(AccountVo count) {
        User userByName = getUserByName(count.getUsername());
        if (userByName != null){
            throw new BisnessException("用户名已存在" );
        }
        User user = new User();
        user.setUsername(count.getUsername());
        user.setPassword(count.getPassword());
        user.setCUser("0");
        user.setCTime(LocalDateTime.now());
        user.setUUser("0");
        user.setUTime(LocalDateTime.now());

        userMapper.insert(user);
    }

    /**
     * 注册功能
     * 查询数据库用户名
     * @param accountName
     * @return
     */
    @Override
    public User getUserByName(String accountName) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", accountName);
        return userMapper.selectOne(queryWrapper);
    }

    /**
     * 登录功能
     * @param username
     * @param password
     */
    public User login(String username, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        queryWrapper.eq("password", password);
        User user = userMapper.selectOne(queryWrapper);
        if (user != null) {
            user.setUTime(LocalDateTime.now());
            userMapper.updateById(user);
        }
        else {
            throw new BisnessException("用户名或密码错误");
        }
        JWTTokenUtil jwtTokenUtil = new JWTTokenUtil();
        String token = jwtTokenUtil.getToken(user.getId().toString());
        return user;
    }

    /**
     * 重置密码功能
     * @param forgetVo
     */
    @Override
    public void resetPwd(ForgetVo forgetVo) {
        //1 查询用户信息
        UserServiceImpl bean = applicationContext.getBean(UserServiceImpl.class);
        User user = bean.getUserByName(forgetVo.getUsername());
        // 2 生成密码
        String pwd = PwdUtil.generatorPassword();
        //3 发送邮件
        bean.sendEmail(pwd,user);
        // 4 更新密码
        userMapper.updateById(user.setPassword(Md5Util.md5Encript(pwd)));

    }

    /**
     * 用多线程异步请求发送邮件
     * @param pwd
     * @param user
     */
    @Async("myExecutor")
    public void sendEmail(String pwd,User user) {
        //3 发送邮件
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setSubject("重置密码");
        simpleMailMessage.setText("您的密码已重置为：" + pwd);
        simpleMailMessage.setTo(user.getEmail());
        simpleMailMessage.setFrom("1054339192@qq.com");

        mailSender.send(simpleMailMessage);
    }
    /**
     * 重置密码功能
     */
    @Override
    public void resetPwdWithAttacchment() {

    }
}
